﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Data;
using SoftSource.Common;

namespace SoftSource.Controls
{
    public sealed class AutoGridEditorViewModel : IAutoGridEditorViewModel
    {
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
        }

        #endregion

        #region IEditor Members

        public Action Cancel { get; set; }
        public Func<object, IObservable<Unit>> Save { get; set; }
        public Func<object, IObservable<Unit>> Apply { get; set; }
        public Action<Exception> Failure { get; set; }

        #endregion        
    
        #region IAutoGridEditorViewModel Members

        private PagedCollectionView values;
        public PagedCollectionView Values
        {
            get { return this.values; }
            private set
            {
                this.values = value;
                this.PropertyChanged.Notify(() => this.Values);
            }
        }

        public Func<object> Add { get; set; }
        public Action<object, Action> Delete { get; set; }
        public Action<DataGridAutoGeneratingColumnEventArgs> OnAutoGenerating { get; set; }
        public SortDescriptionCollection SortDescriptions
        {
            get { return this.Values.SortDescriptions; }
        }

        private IEnumerable collection;
        public IEnumerable Collection
        {
            get { return this.collection; }
            set
            {
                this.collection = value;
                this.Values = new PagedCollectionView(value);
            }
        }

        #endregion
    }
}
